import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

// 创建路由规则
const routes: any = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/LoginView.vue"),
  },
  {
    path: "/reg",
    name: "reg",
    component: () => import("../views/UserRegView.vue"),
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    children: [
      {
        path: "user",
        component: () => import("../views/UserListView.vue"),
      },
      {
        path: "my",
        component: () => import("../views/MyInfoView.vue"),
      },
      {
        path: "prod",
        component: () => import("../views/ProdListView.vue"),
      },
      {
        path: "order",
        component: () => import("../views/OrderListView.vue"),
      },
      {
        path: 'log',
        component: () => import('../views/LogViewList.vue')
      },
    ],
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import("../views/AboutView.vue"),
  },
  {
    path: "/line",
    name: "line",
    component: () => import("../views/DemoLineView.vue"),
  },
  {
    path: "/map",
    name: "map",
    component: () => import("../views/DemoAmapView.vue"),
  },
   
];

// 创建router实例
const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.name === "login") {
    next();
    return;
  }
  if (to.name === "reg") {
    next();
    return;
  }
  // 检查用户是否认证，例如检查token
  let token = window.sessionStorage.getItem("token");
  // to.name
  if (token == null) {
    next({ path: "/login", query: { redirect: to.fullPath } });
  } else {
    next();
  }
});

export default router;
